package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.*;
import com.tanhua.server.service.QuestionService;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.Userservice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private Userservice userservice;

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private Userservice userService;
//查询用户请求头，请求参数
    @GetMapping
    public ResponseEntity user(Long userID){
        //获得id
        if(userID==null){
            userID = UserHolder.getUserId();
        }
          UserInfoVo userInfo=userInfoService.findById(userID);
        log.info("用户信息为:{}",userInfo);
        return ResponseEntity.ok(userInfo);
    }

    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        //设置用户id
        userInfo.setId(Long.valueOf(UserHolder.getUserId()));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
        //
    }

    @PostMapping("/header")
    public ResponseEntity updateheadPhoto(MultipartFile headPhoto) throws IOException {
        userInfoService.updateHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    @GetMapping("/settings")
    public ResponseEntity settings(){
               SettingsVo vo=settingsService.settings();
               return ResponseEntity.ok(vo);
    }

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        //获得前端传来的陌生人问题参数
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }
    @PostMapping("/notifications/setting")
    public  ResponseEntity setting(@RequestBody Map map){
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        //1、调用service查询
        PageResult pr = settingsService.blacklist(page,size);
        //2、构造返回
        return ResponseEntity.ok(pr);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String mobile = UserHolder.getMobile();
        userService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }
    @PostMapping("/phone/checkVerificationCode")
    public  ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        boolean verification=userService.checkVerificationCode(verificationCode);
        Verification verification1 =new Verification();
        verification1.setVerification(verification);
                return ResponseEntity.ok(verification1);
    }
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        userService.updatephone(phone);
        return ResponseEntity.ok(null);
    }

    @GetMapping("/counts")
    public ResponseEntity counts(){
        CountsVo vo= userservice.findCount();
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") int type,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int pagesize){
       PageResult pr= userService.friendsType(type,page,pagesize);
       return ResponseEntity.ok(pr);

    }
}
